<?php 
class News extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->_layout = 'layout/content_layout';
        $this->_view_data = array(
            'module_name' => 'news',
        );
    }
    public function get_side_news($options=array())
    {
        $options['status'] = STATUS_ACTIVE;
        $options['lang'] = $this->_lang;
        $options['sort_by_viewed'] = TRUE;
        $config = get_cache('configurations_' .  $options['lang']);
        $options['limit'] = $config['number_news_per_side'] <> 0 ? $config['number_news_per_side'] : NEWS_PER_SIDE;
        $data = $this->news_model->get_news($options);
        return $data;
    }


    public function get_latest_news($options=array())
    {
        $options['status'] = STATUS_ACTIVE;
        $options['lang'] = $this->_lang;
        $config = get_cache('configurations_' .  $options['lang']);
        $options['limit'] = $config['number_news_per_home'] <> 0 ? $config['number_news_per_home'] : NEWS_PER_HOME;
        $data = $this->news_model->get_news($options);
        return $data;
    }
    
    /**
     * 
     * @param type $para1
     * @param type $para2
     */
    
    public function news_search($para1=NULL, $para2=DEFAULT_LANGUAGE)
    {
        $keyword = $this->db->escape_str($this->input->post('keyword'));
        
        if (isset($keyword) && !empty($keyword)) {
            $this->phpsession->save('news_search', $keyword);
        } else {
            $keyword = $this->phpsession->get('news_search');
        }

        $options = array('keyword'=>$keyword,'lang'=>switch_language($para2),'page'=>$para1,'status'=>STATUS_ACTIVE);
        $config         = get_cache('configurations_' .  $options['lang']);
        $new_per_page   = $config['news_per_page'] <> 0 ? $config['news_per_page'] : NEWS_PER_PAGE;

        $total_row          = $this->news_model->get_news_count($options);
        $total_pages        = (int)($total_row / $new_per_page);
        
        if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
            $base_url = site_url().$options['lang'].'/'.$this->uri->segment(2);
            $uri_segment = 3;
        }else{
            $base_url = site_url().$this->uri->segment(1);
            $uri_segment = 2;
        }
        
        $paging_config = array(
            'base_url'          => $base_url,
            'total_rows'        => $total_row,
            'per_page'          => $new_per_page,
            'uri_segment'       => $uri_segment,   
            'use_page_numbers'  => TRUE,
            'first_link'        => __('IP_paging_first'),
            'last_link'         => __('IP_paging_last'),
            'num_links'         => 1,
        );
        $this->pagination->initialize($paging_config);
        $options['offset'] = ($options['page']>0)?($options['page']-1) * $paging_config['per_page']:0;
        $options['limit']   = $paging_config['per_page'];
        
        $news = $this->news_model->get_news($options);        

        $view_data = array(
            'news'          => $news,
            'category'      => __('IP_search_result'),
            'total_row'     => $total_row,
            'keyword'       => $keyword,
            'title'         => __('IP_search_result'),
            'keywords'      => $keyword,
            'description'   => $keyword,
        );

        $this->_view_data['main_content'] = $this->load->view('news_search',$view_data, TRUE);

        $this->load->view($this->_layout, $this->_view_data, FALSE);
    }
    
    public function news_tags($para1=NULL, $para2=DEFAULT_LANGUAGE, $para3=NULL)
    {
        $options = array('lang'=>switch_language($para2),'page'=>$para1,'tags'=>$para3,'status'=>STATUS_ACTIVE);
        $config         = get_cache('configurations_' .  $options['lang']);
        $new_per_page   = $config['news_per_page'] <> 0 ? $config['news_per_page'] : NEWS_PER_PAGE;

        $total_row          = $this->news_model->get_news_count($options);
        $total_pages        = (int)($total_row / $new_per_page);
        
        if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
            $base_url = site_url().$options['lang'].'/'.$this->uri->segment(2).'/'.$options['tags'];
            $uri_segment = 4;
        }else{
            $base_url = site_url().$this->uri->segment(1).'/'.$options['tags'];
            $uri_segment = 3;
        }
        
        $paging_config = array(
            'base_url'          => $base_url,
            'total_rows'        => $total_row,
            'per_page'          => $new_per_page,
            'uri_segment'       => $uri_segment,   
            'use_page_numbers'  => TRUE,
            'first_link'        => __('IP_paging_first'),
            'last_link'         => __('IP_paging_last'),
            'num_links'         => 1,
        );
        $this->pagination->initialize($paging_config);
        $options['offset'] = ($options['page']>0)?($options['page']-1) * $paging_config['per_page']:0;
        $options['limit']   = $paging_config['per_page'];
        
        $news = $this->news_model->get_news($options);        
        $tags = str_replace('-',' ', $options['tags']);
        $view_data = array(
            'news'          => $news,
            'category'      => $tags,
            'total_row'     => $total_row,
            'title'         => __('IP_tags') . ': ' . $options['tags'],
            'keywords'      => $options['tags'],
            'description'   => $options['tags'],
        );

        $this->_view_data['main_content'] = $this->load->view('news_tags',$view_data, TRUE);

        $this->load->view($this->_layout, $this->_view_data, FALSE);
    }
    
    public function get_list_news_by_cat($para1=NULL, $para2=NULL, $para3=NULL)
    {
        $options = array('cat_id'=>$para1,'page'=>$para2,'lang'=>switch_language($para3),'status'=>STATUS_ACTIVE);
        $config         = get_cache('configurations_' .  $options['lang']);
        $new_per_page   = $config['news_per_page'] <> 0 ? $config['news_per_page'] : NEWS_PER_PAGE;

        $category = $this->news_categories_model->get_news_categories(array('id'=>$options['cat_id']));
//        if($category->grid == STATUS_ACTIVE){
//            $new_per_page = 12;
//        }

//        $child_categories = $this->news_categories_model->get_news_categories(array('parent_id'=>$options['cat_id']));
        if(!empty($child_categories) && !empty($category) && $category->grid == STATUS_ACTIVE){
            $total_row          = count($child_categories);
            $total_pages        = (int)($total_row / $new_per_page);
            
            if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
                $base_url = site_url().$options['lang'].'/'.$this->uri->segment(2);
                $uri_segment = 3;
            }else{
                $base_url = site_url().$this->uri->segment(1);
                $uri_segment = 2;
            }
            
            $paging_config = array(
                'base_url'          => $base_url,
                'total_rows'        => $total_row,
                'per_page'          => $new_per_page,
                'uri_segment'       => $uri_segment,   
                'use_page_numbers'  => TRUE,
                'first_link'        => __('IP_paging_first'),
                'last_link'         => __('IP_paging_last'),
                'num_links'         => 1,
            );
            $this->pagination->initialize($paging_config);
            $options['offset'] = ($options['page']>0)?($options['page']-1) * $paging_config['per_page']:0;
            $options['limit']   = $paging_config['per_page'];
            
            $options_2 = array(
                'parent_id'=>$options['cat_id'],
                'lang' => $options['lang'],
                'offset' => $options['offset'],
                'limit' => $options['limit'],
            );
            
            $data = $this->news_categories_model->get_news_categories($options_2);
            
            //current menu
            $check_id = $category->parent_id;

            if($check_id <> 0){
                while ($check_id <> 0){
                    $parent_category = $this->news_categories_model->get_news_categories(array('id'=>$check_id));
                    if($parent_category->parent_id == 0){
                        if(SLUG_ACTIVE==0){
                            $active_menu = '/'.url_title($parent_category->category, 'dash', TRUE) . '-n' .$parent_category->id;
                        }else{
                            $active_menu = '/'.$parent_category->slug;
                        }
                        break;
                    }else{
                        $check_id = $parent_category->parent_id;
                    }
                }
            }else{
                $active_menu = NULL;
            }

            if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
                $current_menu = '/' . $this->uri->segment(2);
            }else{
                $current_menu = '/' . $this->uri->segment(1);
            }
            
            $view_data = array(
                'data'          => $data,
                'category'      => $category->category,
                'category_id'   => $category->id,
//                'category_summary' => $category->summary,
//                'category_content' => $category->content,
                'title'         => ($category->meta_title <> '')?$category->meta_title:$category->category,
                'keywords'      => ($category->meta_keywords <> '')?$category->meta_keywords:$category->category,
                'description'   => ($category->meta_description <> '')?$category->meta_description:$category->category,
                'current_menu'  => $current_menu,
                'active_menu'   => $active_menu,
            );
            
            $this->_view_data['main_content'] = $this->load->view('news_categories_grid',$view_data, TRUE);
            
        }else{
            $total_row          = $this->news_model->get_news_count($options);
            $total_pages        = (int)($total_row / $new_per_page);

            if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
                $base_url = site_url().$options['lang'].'/'.$this->uri->segment(2);
                $uri_segment = 3;
            }else{
                $base_url = site_url().$this->uri->segment(1);
                $uri_segment = 2;
            }

            $paging_config = array(
                'base_url'          => $base_url,
                'total_rows'        => $total_row,
                'per_page'          => $new_per_page,
                'uri_segment'       => $uri_segment,   
                'use_page_numbers'  => TRUE,
                'first_link'        => __('IP_paging_first'),
                'last_link'         => __('IP_paging_last'),
                'num_links'         => 1,
            );
            $this->pagination->initialize($paging_config);
            $options['offset'] = ($options['page']>0)?($options['page']-1) * $paging_config['per_page']:0;
            $options['limit']   = $paging_config['per_page'];

            $news = $this->news_model->get_news($options);

            //current menu
            $check_id = $category->parent_id;

            if($check_id <> 0){
                while ($check_id <> 0){
                    $parent_category = $this->news_categories_model->get_news_categories(array('id'=>$check_id));
                    if($parent_category->parent_id == 0){
                        if(SLUG_ACTIVE==0){
                            $active_menu = '/'.url_title($parent_category->category, 'dash', TRUE) . '-n' .$parent_category->id;
                        }else{
                            $active_menu = '/'.$parent_category->slug;
                        }
                        break;
                    }else{
                        $check_id = $parent_category->parent_id;
                    }
                }
            }else{
                $active_menu = NULL;
            }

            if((!empty($options['lang']) && $options['lang'] <> DEFAULT_LANGUAGE) || $this->uri->segment(1) == DEFAULT_LANGUAGE){
                $current_menu = '/' . $this->uri->segment(2);
            }else{
                $current_menu = '/' . $this->uri->segment(1);
            }

            $view_data = array(
                'news'          => $news,
                'category'      => $category->category,
                'category_id'   => $category->id,
//                'category_summary' => $category->summary,
//                'category_content' => $category->content,
                'title'         => ($category->meta_title <> '')?$category->meta_title:$category->category,
                'keywords'      => ($category->meta_keywords <> '')?$category->meta_keywords:$category->category,
                'description'   => ($category->meta_description <> '')?$category->meta_description:$category->category,
                'current_menu'  => $current_menu,
                'active_menu'   => $active_menu,
            );

            if($category->grid == STATUS_ACTIVE){
                $this->_view_data['main_content'] = $this->load->view('news_grid',$view_data, TRUE);
            }else{
                $this->_view_data['main_content'] = $this->load->view('news',$view_data, TRUE);
            }
        }
        $this->load->view($this->_layout, $this->_view_data, FALSE);
    }
    
    public function get_news_detail($para1=NULL, $para2=NULL)
    {
        $options = array('id'=>$para1); //,'status'=>STATUS_ACTIVE

        $lang = switch_language($para2);
        
        $this->news_model->update_news_view($options['id']);
        
        $news = $this->news_model->get_news($options);
        
        $news_same = $this->get_list_news_same(array('cat_id'=>$news->cat_id,'current_id'=>$options['id'],'limit'=>NEWS_PER_LIST));
        
        //current menu
        $category = $this->news_categories_model->get_news_categories(array('id'=>$news->cat_id));
        if(!empty($category)){
            if(SLUG_ACTIVE==0){
                $category_slug = '';
            }else{
                $category_slug = $category->slug;
            }
            $check_id = $category->parent_id;
        }else{
            $category_slug = '';
            $check_id = 0;
        }

        if($check_id <> 0){
            while ($check_id <> 0){
                $parent_category = $this->news_categories_model->get_news_categories(array('id'=>$check_id));
                if($parent_category->parent_id == 0){
                    if(SLUG_ACTIVE==0){
                        $active_menu = '/'.url_title($parent_category->category, 'dash', TRUE) . '-n' .$parent_category->id;
                    }else{
                        $active_menu = '/'.$parent_category->slug;
                    }
                    break;
                }else{
                    $check_id = $parent_category->parent_id;
                }
            }
        }else{
            $active_menu = NULL;
        }
        if(SLUG_ACTIVE==0){
            $current_menu = '/'.url_title($category->category, 'dash', TRUE) . '-n' .$category->id;
        }else{
            $current_menu = '/'.$category_slug;
        }        
        
        if(!empty($lang) && $lang <> DEFAULT_LANGUAGE){
            $current_menu = '/'.$lang.$current_menu;
        }else{
            $current_menu = $current_menu;
        }
        
        $view_data = array(
            'news'          => $news,
            'category'      => $news->category,
            'category_slug' => $category_slug,
            'title'         => ($news->meta_title <> '')?$news->meta_title:$news->title,
            'keywords'      => ($news->meta_keywords <> '')?$news->meta_keywords:$news->title,
            'description'   => ($news->meta_description <> '')?$news->meta_description:$news->summary,
            'current_menu'  => $current_menu,
            'active_menu'   => $active_menu,
            'news_same'     => $news_same,
            'tags'          => convert_tags_to_array($news->tags),
        );

        $this->_view_data['main_content'] = $this->load->view('news_detail',$view_data, TRUE);

        $this->load->view($this->_layout, $this->_view_data, FALSE);
        
    }
    
    /**
     * tin lien quan / cung loai
     * @param type $options
     * @return type
     */
    public function get_list_news_same($options=array())
    {
        $options['status'] = STATUS_ACTIVE;
//        $options['random'] = TRUE;
        $options['lang'] = $this->_lang;
        $news1 = $this->news_model->get_news(array('sort_by_id_high'=>TRUE,'cat_id'=>$options['cat_id'],'current_id'=>$options['current_id'],'limit'=>NEWS_PER_RELATED,'lang'=>$options['lang'],'status'=>$options['status']));
        $news2 = $this->news_model->get_news(array('sort_by_id_low'=>TRUE,'cat_id'=>$options['cat_id'],'current_id'=>$options['current_id'],'limit'=>NEWS_PER_RELATED,'lang'=>$options['lang'],'status'=>$options['status']));
        if (!empty($news1) && !empty($news2)) {
            $news = array_merge($news1, $news2);
        } elseif (!empty($news1) && empty($news2)) {
            $news = $news1;
        } elseif (empty($news1) && !empty($news2)) {
            $news = $news2;
        } else {
            $news = NULL;
        }
        $view_data = array(
            'category' => __('IP_news_same'),
            'news' => $news,
        );
        return $this->load->view('news_same', $view_data, TRUE);
    }
    

}
